package com.example.lenovo.newsdemo;

import android.content.Context;

import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;

class MyAdapter extends RecyclerView.Adapter<MyViewHolder>{
    private NewsData newsData;
    private Context context;
    private OnItemClickListener mOnItemClickListener;

    //新建接口
    public interface OnItemClickListener{
        void onItemClick(View view,int position);
    }
    public void setmOnItemClickListener(OnItemClickListener mOnItemClickListener){
        this.mOnItemClickListener=mOnItemClickListener;
    }


    public MyAdapter(Context context,NewsData newsData){
        this.context = context;
        this.newsData = newsData;

    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.news_item,parent,false);
         MyViewHolder myViewHolder = new MyViewHolder(view);

        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        NewsData.ResultBean.DataBean dataBean = newsData.getResult().getData().get(position);
        holder.news_tv_title.setText(dataBean.getTitle());
        holder.news_tv_author.setText(dataBean.getAuthor_name());
        holder.news_tv_date.setText(dataBean.getDate());
        Glide.with(context).load(Uri.parse(dataBean.getThumbnail_pic_s())).into(holder.news_iv_img);

        if(mOnItemClickListener!=null){
            //为ItemView设置监听器
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int position = holder.getLayoutPosition();
                    mOnItemClickListener.onItemClick(holder.itemView,position);
                }
            });
        }

    }


    @Override
    public int getItemCount() {
        return newsData.getResult().getData().size();
    }
}
